package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.dto.WbCompanyBlacklistCountProvinceDTO;
import org.dromara.system.domain.dto.WbProvinceDTO;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbProvinceBo;
import org.dromara.system.domain.vo.WbProvinceVo;
import org.dromara.system.domain.WbProvince;
import org.dromara.system.mapper.WbProvinceMapper;
import org.dromara.system.service.IWbProvinceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 省份Service业务层处理
 *
 * @author Lion Li
 * @date 2024-04-12
 */
@RequiredArgsConstructor
@Service
public class WbProvinceServiceImpl implements IWbProvinceService {

    private final WbProvinceMapper baseMapper;

    @Override
    public List<WbCompanyBlacklistCountProvinceDTO> provinceBlacklistNum(String companyName) {
        return baseMapper.selectProvinceBlacklistNum(companyName);
    }

    /**
     * 提供省份名查id
     */

    @Override
    public Long queryByName(String name) {
        return baseMapper.selectName(name);
    }

    /**
     * 查询省份
     */
    @Override
    public WbProvinceVo queryById(Long provinceId){
        return baseMapper.selectVoById(provinceId);
    }

    /**
     * 查询省份列表
     */
    @Override
    public TableDataInfo<WbProvinceVo> queryPageList(WbProvinceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbProvince> lqw = buildQueryWrapper(bo);
        Page<WbProvinceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询省份列表
     */
    @Override
    public List<WbProvinceVo> queryList(WbProvinceBo bo) {
        LambdaQueryWrapper<WbProvince> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbProvince> buildQueryWrapper(WbProvinceBo bo) {
        LambdaQueryWrapper<WbProvince> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getProvinceName()), WbProvince::getProvinceName, bo.getProvinceName());
        return lqw;
    }

    /**
     * 新增省份
     */
    @Override
    public Boolean insertByBo(WbProvinceBo bo) {
        WbProvince add = MapstructUtils.convert(bo, WbProvince.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProvinceId(add.getProvinceId());
        }
        return flag;
    }

    /**
     * 修改省份
     */
    @Override
    public Boolean updateByBo(WbProvinceBo bo) {
        WbProvince update = MapstructUtils.convert(bo, WbProvince.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbProvince entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除省份
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Integer querySonNum(Collection<Long> provinceIds) {
        return baseMapper.selectSonNum(provinceIds);
    }

    @Override
    public TableDataInfo<WbProvinceVo> queryListZYQ(WbProvinceDTO dto, PageQuery pageQuery) {
        Page<WbProvinceVo> result = baseMapper.getProvincesWithCitiesAndDistricts(dto,pageQuery.build());
        return TableDataInfo.build(result);
    }
}
